package org.sosie2.service.framework;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.apache.axis.Message;
import org.apache.axis.message.SOAPEnvelope;
import org.mule.umo.UMOEventContext;
import org.mule.umo.lifecycle.Callable;


/**
 * Similar to the AbstractService, this is an abstract class that security services shall extend (if they are to be realized as components in Mule).
 *   
 * @author Gabriela
 */
public abstract class MuleAbstractService extends AbstractService implements Callable {
		
	public Object onCall(UMOEventContext context) throws Exception {
		Message req = null;
		Message resp = Utils.createMessage("");;
		
		Object muleMsg = context.getTransformedMessage();
		if (muleMsg instanceof SOAPEnvelope) {			
			req = new Message ((SOAPEnvelope) muleMsg);
		} else if (muleMsg instanceof String) {
			InputStream in = new ByteArrayInputStream(((String)muleMsg).getBytes());				
        	req = new Message(in); 
		} else {
			req = new Message((byte[])muleMsg);
		}
			
		resp = invoke(req, resp);
		
		//TODO: Change this! In moment it only supports messages without attachments!
		return resp.getSOAPEnvelope();
	}		


	public abstract Message invoke(Message req, Message resp) throws Exception;	
}
